package com.forum.message.controller;

import com.forum.common.response.Result;
import com.forum.message.model.dto.MessageDTO;
import com.forum.message.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/**
 * 消息控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
@Tag(name = "消息接口", description = "提供消息相关的API")
public class MessageController {
    
    private final MessageService messageService;
    
    /**
     * 获取当前用户的消息列表
     * 
     * @param userId 用户ID
     * @param isRead 是否已读（可选）
     * @param type 消息类型（可选）
     * @param page 页码
     * @param size 每页大小
     * @return 消息分页列表
     */
    @GetMapping
    @Operation(summary = "获取用户消息列表", description = "分页获取用户的消息列表，支持按已读状态和类型筛选")
    public Result<Page<MessageDTO>> getUserMessages(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "是否已读") @RequestParam(required = false) Boolean isRead,
            @Parameter(description = "消息类型") @RequestParam(required = false) String type,
            @Parameter(description = "页码") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int size) {
        
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by("createdTime").descending());
        return Result.success(messageService.getUserMessages(userId, isRead, type, pageRequest));
    }
    
    /**
     * 获取用户未读消息数量
     * 
     * @param userId 用户ID
     * @param type 消息类型（可选）
     * @return 未读消息数量
     */
    @GetMapping("/unread/count")
    @Operation(summary = "获取未读消息数量", description = "获取用户的未读消息数量，可按类型筛选")
    public Result<Long> getUnreadCount(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "消息类型") @RequestParam(required = false) String type) {
        
        return Result.success(messageService.getUnreadCount(userId, type));
    }
    
    /**
     * 标记单条消息为已读
     * 
     * @param messageId 消息ID
     * @param userId 用户ID
     * @return 更新后的消息
     */
    @PutMapping("/{messageId}/read")
    @Operation(summary = "标记消息为已读", description = "将指定的消息标记为已读状态")
    public Result<MessageDTO> markAsRead(
            @Parameter(description = "消息ID") @PathVariable Long messageId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        
        return Result.success(messageService.markAsRead(messageId, userId));
    }
    
    /**
     * 标记所有消息为已读
     * 
     * @param userId 用户ID
     * @param type 消息类型（可选）
     * @return 标记为已读的消息数量
     */
    @PutMapping("/read-all")
    @Operation(summary = "标记所有消息为已读", description = "将用户所有未读消息标记为已读状态，可按类型筛选")
    public Result<Integer> markAllAsRead(
            @Parameter(description = "用户ID") @RequestParam Long userId,
            @Parameter(description = "消息类型") @RequestParam(required = false) String type) {
        
        return Result.success(messageService.markAllAsRead(userId, type));
    }
    
    /**
     * 删除消息
     * 
     * @param messageId 消息ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/{messageId}")
    @Operation(summary = "删除消息", description = "删除指定的消息")
    public Result<Void> deleteMessage(
            @Parameter(description = "消息ID") @PathVariable Long messageId,
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        
        messageService.deleteMessage(messageId, userId);
        return Result.success();
    }
    
    /**
     * 删除所有消息
     * 
     * @param userId 用户ID
     * @return 删除的消息数量
     */
    @DeleteMapping
    @Operation(summary = "删除所有消息", description = "删除用户的所有消息")
    public Result<Integer> deleteAllMessages(
            @Parameter(description = "用户ID") @RequestParam Long userId) {
        
        return Result.success(messageService.deleteAllMessages(userId));
    }
} 